# coding=utf-8
#

"""
定时同步币安的交易订单
"""

import os
import json
import time
from uuid import uuid4
from datetime import datetime
from datetime import timedelta

from django.core.management.base import BaseCommand

from iminer.proxy.qubic import QubicProxy

from iminer.utils import check_pid

from iminer.agent.models import Pool
from iminer.agent.models import Agent
from iminer.account.models import Account


class Command(BaseCommand):
    help = "定时将矿池中的Agent信息同步到本地"

    def handle(self, *args, **options):
        """ 定时将矿池中的Agent信息同步到本地 """
        task_name = "pid/cron_create_instance"

        try:
            old_pid = int(open(task_name).read())
        except FileNotFoundError:
            old_pid = 0
        except ValueError:
            old_pid = 0

        cur_pid = os.getpid()
        if not old_pid or not check_pid(old_pid):
            with open(task_name, "w") as fp:
                fp.write(str(cur_pid))

